<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Project;
use app\api\model\v1\ProjectDetail;
use app\api\model\v1\ProjectSecondLevel;
use app\api\model\v1\ProjectType;
use app\api\model\v1\Template;
use app\api\model\v1\TemplateType;
use app\BaseController;
use think\Request;

class ProjectCon extends BaseController
{

    use SearchDataForModel;

    /**
     * @User: 刘海龙
     * @Date: 2021/11/11
     * @Time: 14:19
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源列表 项目
     */
    public function getProjectList(){
        $limit = $this->request->param('limit',10);
        $map = [];
        if ($this->request->has('is_release') && !empty($this->request->param('is_release'))) {
            $map[] = ['is_release', '=', $this->request->param('is_release')];
        }
        if ($this->request->has('status') && !empty($this->request->param('status'))) {
            $map[] = ['status', '=', $this->request->param('status')];
        }
        $res = $this->search(new Project(),$map,$limit,'desc',['projectType']);
        return $this->response($res);
    }


    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/11/11
     * @Time: 15:29
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除项目
     */
    public function delProject($id){
        $res = Project::with(['projectType', 'projectSecond', 'projectDetail','projectSample'])->find($id);
        if (is_null($res)) {
            return $this->response(501, '无法找到该模版');
        }
        $res->together(['projectType','projectSecond', 'projectDetail','projectSample'])->force()->delete();
        //日志
        AdminLog::record('删除模版', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }
    /**
     * @User: 刘海龙
     * @Date: 2021/11/11
     * @Time: 11:35
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 创建项目
     */
    public function saveProjectAll(){
        $data = $this->request->param();
        $data['create_id'] = $this->request->uid;
        $template = Template::find($data['template_id']);
        if (is_null($template)){
            return $this->response(403,'模版不存在');
        }
        //创建项目
       $project_id = Project::create($data)->id;
        //根据模版id找到一级大类
        $template_type = TemplateType::with(['templateSecondLevel' => ['templateDetail']])->where(['template_id' => $data['template_id']])->select()->toArray();
        //调用同步方法
        $is_result = Project::synchronousData($template_type, $project_id,$this->request->uid);
        if ($is_result === 200) {
            AdminLog::record('创建项目', $this->request->param(), $this->request->uid, $this->request->username);
            return $this->response();
        } else {
            // 真实删除
            Project::destroy($project_id,true);
            return $this->response(403, '处理数据失败');
        }
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/11/16
     * @Time: 9:49
     * @return \think\response\Json
     * 编辑项目
     */
    public function saveEditProject(){
        $data = $this->request->param();
        Project::update($data);
        AdminLog::record('编辑项目', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/11/16
     * @Time: 10:07
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 检查项目
     */
    public function checkProject($id)
    {
        $res = Project::with(['projectType'])->find($id);
        if (is_null($res)) {
            return $this->response(501, '无法找到该数据');
        }
        $type = $res['projectType']->toArray();
        if (empty($type)) {
            return $this->response(501, '暂无数据,无法打开');
        }
        return $this->response($type);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/11/16
     * @Time: 10:12
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 根据项目id 获取对应的分类
     */
    public function projectTypeList(){
        $limit = $this->request->param('limit', 10);
        $id = $this->request->param('id');
        $map[] = array('project_id', '=', $id);
        $res = $this->search(new ProjectType(), $map, $limit, 'asc', ['projectSecondLevel.projectDetail']);
        return $this->response($res);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/11/16
     * @Time: 10:23
     * @return \think\response\Json
     * 更改二级数据
     */
    public function updateProjectSecondLevel(){
        $data = $this->request->param();

        ProjectSecondLevel::update($data);
        unset($data['projectDetail']);
        //日志
        AdminLog::record('更新模版二级类别', $data, $this->request->uid, $this->request->username);
        return $this->response();
    }


    /**
     * @User: 刘海龙
     * @Date: 2021/9/24
     * @Time: 17:39
     * @return \think\response\Json
     * 大类别更新
     */
    public function updateProjectType()
    {
        $data = $this->request->param();
        ProjectType::update([
            'id' => $data['id'],
            'project_type_name' => $data['project_type_name']
        ]);
        //日志
        AdminLog::record('更新项目类别', $this->request->only(['id', 'project_type_name']), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/11/16
     * @Time: 10:32
     * @return \think\response\Json
     * 更改项目项目指标
     */
    public function updateProjectDetail(){
        $data = $this->request->param();

        ProjectDetail::update($data);
        //日志
        AdminLog::record('更新项目详情', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }
}
